<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControlFake" class="tabControl"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentscroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <recommend-view :recommends="recommends" />
      <featrue-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControlReal"
      />
      <goods-list :goods="showGoods" />
    </scroll>
    <bakc-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import { getHomeMultidata, getHomeGoods } from "network/home";
import { debounce } from "../../components/common/utils";

import NavBar from "../../components/common/navbar/NavBar.vue";
import RecommendView from "./childComps/RecommendView.vue";
import FeatrueView from "./childComps/FeatrueView.vue";
import TabControl from "../../components/content/tabControl/TabControl.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";
import Scroll from "../../components/common/scroll/Scroll.vue";
import HomeSwiper from "./childComps/HomeSwiper.vue";
import BakcTop from "../../components/content/backTop/BakcTop.vue";

export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] }
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffSetTop: 0,
      isTabFixed: false,
      saveY:0
    };
  },
  components: {
    NavBar,
    RecommendView,
    FeatrueView,
    TabControl,
    GoodsList,
    Scroll,
    HomeSwiper,
    BakcTop
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    }
  },
  created() {
    this.getmultidata();
    //请求商品数据
    this.gethomegoods("pop");
    this.gethomegoods("new");
    this.gethomegoods("sell");
  },
  methods: {
    //事件监听相关

    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControlFake.currentIndex=index;
      this.$refs.tabControlReal.currentIndex=index;
    },
    contentscroll(position) {
      //1.判断backTop是否显示
      this.isShowBackTop = -position.y > 1000 ? true : false;

      //2.决定tabControl是否吸顶（position：fixed）
      this.isTabFixed = -position.y > this.tabOffSetTop;
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    loadMore() {
      this.gethomegoods(this.currentType);
    },
    swiperImageLoad() {
      this.tabOffSetTop = this.$refs.tabControlReal.$el.offsetTop;
    },
    //网络请求相关
    getmultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    gethomegoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    }
  },
  mounted() {
    //1.图片加载完成的事件监听
    let refresh = debounce(this.$refs.scroll.refresh, 500);
    //监听item图片加载完成
    this.$bus.$on("itemImageLoad", () => {
      refresh();
    });

    //2.获取tabControl的offsetTop
    //所有组件都有一个属性：$el;用于获取组件中的元素
    this.tabOffSetTop=this.$refs.tabControlReal.$el.offsetTop;
  },
  // activated(){
  //   this.$refs.scroll.scrollTo(0,this,this.saveY,0)
  // },
  // deactivated(){
  //   this.saveY=this.$refs.scroll.getScrollY
  // },
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  left: 0;
  top: 0;
  right: 0;
  z-index: 9; */
}

.content {
  overflow: hidden;
  /* height: calc(100% - 49px); */
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
.tabControl{
  position: relative;
  z-index: 9;
}
</style>
